package com.cgbl.cg.vm.login

import android.app.Application
import android.text.TextUtils
import androidx.databinding.ObservableField
import api.UserServiceFactory
import com.blankj.utilcode.util.StringUtils
import com.cgbl.cg.R
import com.robot.base.base.vm.GLBaseViewModel
import com.robot.base.common.api.AbstractViewModelSubscriber
import com.robot.base.util.StringUtil
import com.robot.base.util.ToastUtils
import com.robot.base.utils.JobUtil
import me.goldze.mvvmhabit.binding.command.BindingAction
import me.goldze.mvvmhabit.binding.command.BindingCommand
import me.goldze.mvvmhabit.http.BaseResponse

class ForgotPasswordViewModel(application: Application) : GLBaseViewModel(application) {
    val codeText = ObservableField(StringUtils.getString(R.string.app_en_tips_19))
    val codeEnable = ObservableField(true)
    val code = ObservableField("")
    val phone = ObservableField("")
    val password = ObservableField("")
    val sendCodeCommand = BindingCommand<Any>(BindingAction {
        if (!StringUtil.checkPhone(phone.get())) {
            ToastUtils.showShort("请输入正确的手机号码")
            return@BindingAction
        }
        setViewData()
    })


    val confirmCommand = BindingCommand<Any>(BindingAction {
        resetPassword()
    })


    private fun setViewData() {
        codeEnable.set(false)
        val job = JobUtil.countDownCoroutines(
            60, {
                codeText.set("${it}${StringUtils.getString(R.string.app_en_tips_23)}")
            },
            {
                codeText.set(StringUtils.getString(R.string.app_en_tips_20))
                codeEnable.set(true)
            }
        )
        job?.start()
        sendSmsVerifyCode()
    }

    private fun sendSmsVerifyCode() {
        UserServiceFactory.sendSmsVerifyCode(
            mapOf(
                "mobile" to phone.get(),
                "scene" to "1"
            )
        ).subscribe(object : AbstractViewModelSubscriber<BaseResponse<String>>(this) {
            override fun onNext(t: BaseResponse<String>) {
                ToastUtils.showShort("发送成功")
            }
        })
    }


    private fun resetPassword() {
        if ((password.get()?.length ?: 0) < 8 || (password.get()?.length ?: 0) > 18) {
            ToastUtils.showShort(StringUtils.getString(R.string.app_error_8))
            return
        }
        if (!StringUtil.checkPhone(phone.get())) {
            ToastUtils.showShort("请输入正确的手机号码")
            return
        }

        if (TextUtils.isEmpty(code.get())) {
            ToastUtils.showShort("验证码不能为空")
            return
        }
        UserServiceFactory.resetPassword(
            mapOf(
                "code" to code.get(),
                "mobile" to phone.get(),
                "password" to password.get()
            )
        ).subscribe(object : AbstractViewModelSubscriber<BaseResponse<String>>(this) {
            override fun onNext(t: BaseResponse<String>) {
                ToastUtils.showShort(t.msg)
                finish()
            }
        })
    }
}